package com.twu.soloproject;

import java.util.Collection;
import java.util.HashSet;

/**
 * Created with IntelliJ IDEA.
 * User: thoughtworker
 * Date: 10/10/13
 * Time: 1:28 PM
 * To change this template use File | Settings | File Templates.
 */
public class League {


    private Collection<Player> playerList;
    private Collection<Team> teamList;

    public League(Collection<Player> playerList, Collection<Team> teams) {
        this.playerList = playerList;
        this.teamList = teams;
    }

    public String getPlayersList() {
        String listingPlayers = "Players:\n";
        String listingCoaches = "Coaches:\n";
        Collection<String> coachList = new HashSet<String>();
        for (Player player: playerList) {
            coachList.add(player.getTeam().getCoachDetails());
            listingPlayers += player.displayInfo() + "\n";
        }

        for (String coach: coachList) {
            listingCoaches += coach;
        }

        return listingPlayers + "\n" + listingCoaches;

    }

    public boolean isPlayerInLeague(String playerName) {
        for (Player player: playerList) {
            if (player.getName().equals(playerName)) {
                return true;
            }
        }
        return false;

    }

    public String getTeamDetails(String teamName) {
        for (Team team: teamList) {
            if(team.getTeamName().equals(teamName)) {
                return team.getTeamDetails();
            }
        }
        return teamName + " does not exist in the league.\n";
    }

    public String searchPlayersByAge(String age) {
        String ageBasedPlayerInfo = "";
        Integer ageSearch = Integer.parseInt(age);
        for (Player player: playerList) {
            if (player.getAge() > ageSearch) {
                ageBasedPlayerInfo += player.displayInfo();
            }
        }
        return ageBasedPlayerInfo;
    }
}
